//
//  SubViewController.m
//  GIFShow
//
//  Created by lanou11air029 on 15/3/31.
//  Copyright (c) 2015年 CK. All rights reserved.
//

#import "SubViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import "MYCommentModel.h"
#import <CommonCrypto/CommonCrypto.h>
#import "MySettingOptions.h"
#import "CustomButton.h"
#import "CommentCell.h"
#import <MRProgress/MRProgress.h>
@interface SubViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) MPMoviePlayerController * moviePlayer;
@property (nonatomic, strong) UIImageView  * userHeadIcon;
//用户名
@property (nonatomic, strong) UILabel      * userNameLabel;
// 视频创建日期
@property (nonatomic, strong) UILabel      * createDateLabel;
@property (nonatomic, strong) UIImageView  * mainImageView;
//内容
@property (nonatomic, strong) UILabel      * mainLabel;
@property (nonatomic, strong) CustomButton * dingButton;
@property (nonatomic, strong) UIButton     * moreButton;
@property (nonatomic, strong) UIButton     * shareButton;
@property (nonatomic, strong) CustomButton * favourButton;

@end

@implementation SubViewController
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
//    [self.moviePlayer play];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;

}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

-(void)dealloc{
    _tableView.delegate = nil;
}
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        [_tableView registerClass:[CommentCell class] forCellReuseIdentifier:@"comment"];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeTop;
    self.automaticallyAdjustsScrollViewInsets = NO;

    self.view.backgroundColor = [UIColor lightGrayColor];
    [UIView animateWithDuration:0.8 animations:^{
        self.view.backgroundColor=[UIColor whiteColor];
    }];
//    [self.navigationController.navigationBar setBackgroundColor:[UIColor yellowColor]];
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"translucentBar"] forBarMetrics:UIBarMetricsCompact];
//    self.navigationController.navigationBar.shadowImage = [UIImage imageNamed:@"translucentBar"];
//    for (UIImageView * shadowImage in self.navigationController.navigationBar.subviews) {
//        shadowImage.hidden = YES;
//    }
//    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithWhite:1 alpha:0.6];
//    [self.navigationController.navigationBar setBackgroundColor:];
//    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    [self setupSubviews];
    UIImage * backImage = [UIImage imageNamed:@"back_nav.png"];
    
    UIBarButtonItem * leftBackButton = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(didClickBackButton:)];

    
    self.navigationItem.leftBarButtonItem = leftBackButton;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
    self.navigationController.navigationBar.tintColor = [UIColor redColor];
    
    

//    // Do any additional setup after loading the view.
//    [self setupSubviews];
//    [self configGestures];
}
-(UIImage *)getImageFromImage:(UIImage *)image{
    UIImage * renderImage =[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    CGRect backrect = CGRectMake(0, 0, 30, 30);
    UIGraphicsBeginImageContext(backrect.size);
    [renderImage drawInRect:backrect];
    UIImage * returnImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return returnImage;
}

-(void)setupSubviews{
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor lightGrayColor];
    [UIView animateWithDuration:0.5 animations:^{
        self.tableView.backgroundColor = [UIColor whiteColor];
    }];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setImage:[UIImage imageNamed:@"back_nav.png"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 64, 64);
    [self.view addSubview:button];
    [button addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.commentList.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentCell * cell = [tableView dequeueReusableCellWithIdentifier:@"comment" forIndexPath:indexPath];
    MYCommentModel * commentItem = self.commentList[indexPath.row];
    cell.commentModel = commentItem;

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat header = self.view.bounds.size.width * 0.1;
//    CGRect rect = CGRectMake(0, 0, self.view.frame.size.width-40, 1000);
//    NSDictionary * attribute = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName];
    MYCommentModel * commentModel = self.commentList[indexPath.row];
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, 100)];
    label.text = commentModel.content;
    label.font = [UIFont systemFontOfSize:14];
    label.numberOfLines = 0;
    [label sizeToFit];
    CGRect textRect = label.frame;
//    CGRect textRect = [commentModel.content boundingRectWithSize:rect.size options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil];
    return header + textRect.size.height +30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return self.movieSize.height+100;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    MySettingOptions * option = [MySettingOptions defaultOptions];

    self.mainImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.movieSize.width, self.movieSize.height)];
    self.mainImageView.clipsToBounds = YES;
    self.mainImageView.contentMode = UIViewContentModeScaleAspectFill;
    MRProgressOverlayView * progressView = [MRProgressOverlayView showOverlayAddedTo:self.mainImageView title:@"努力加载中..." mode:MRProgressOverlayViewModeDeterminateHorizontalBar animated:YES];

    progressView.backgroundColor = [UIColor clearColor];
    [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:self.movieStr] placeholderImage:self.movieImage options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        float rate = receivedSize*1.0 / expectedSize;
        if (rate>0 && rate<1) {
            [progressView setProgress:rate];
        }
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [MRProgressOverlayView dismissOverlayForView:self.mainImageView animated:YES];
    }];
    
    [view addSubview:self.mainImageView];
    //用户头像
    CGRect contentViewBounds = self.mainImageView.frame;
    self.userHeadIcon = [[UIImageView alloc]initWithFrame:CGRectMake(contentViewBounds.size.width*0.05, contentViewBounds.origin.y+view.bounds.size.width*0.03+self.mainImageView.frame.size.height, contentViewBounds.size.width*0.13, contentViewBounds.size.width*0.13)];
    self.userHeadIcon.layer.cornerRadius = contentViewBounds.size.width*0.065;
    self.userHeadIcon.layer.masksToBounds = YES;
    self.userHeadIcon.layer.borderColor = [UIColor colorWithRed:0.7 green:0.8 blue:1 alpha:1].CGColor;
    self.userHeadIcon.layer.borderWidth = 1;
    
    [view addSubview:self.userHeadIcon];
    NSDictionary * userinfo = self.gifModel.userinfo;
    [self.userHeadIcon sd_setImageWithURL:[NSURL URLWithString:[userinfo objectForKey:@"avatar"]] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    //用户名
    
    self.userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.userHeadIcon.frame.origin.x+self.userHeadIcon.frame.size.width+view.frame.size.width*0.05, self.userHeadIcon.frame.origin.y, contentViewBounds.size.width * 0.7, 24)];
    self.userNameLabel.text = @"某人";
    self.userNameLabel.font = [UIFont systemFontOfSize:option.userFontValue];
    [view addSubview:self.userNameLabel];
    self.userNameLabel.text = [userinfo objectForKey:@"nickname"];
    // 视频创建日期
    self.createDateLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.userNameLabel.frame.origin.x, self.userNameLabel.frame.origin.y+self.userNameLabel.frame.size.height, self.userNameLabel.frame.size.width, self.userNameLabel.frame.size.height)];
    self.createDateLabel.font = [UIFont systemFontOfSize:option.otherFontValue];
    self.createDateLabel.textColor = [UIColor colorWithRed:0.8 green:0.6 blue:0.25 alpha:0.9];
    [view addSubview:self.createDateLabel];
    self.createDateLabel.text = self.datePop;
    //内容
    self.mainLabel = [[UILabel alloc]initWithFrame:CGRectMake(2, 2, view.bounds.size.width*0.9, 40)];
    self.mainLabel.font = [UIFont systemFontOfSize:option.contentFontValue];
    [view addSubview:self.mainLabel];
    self.mainLabel.numberOfLines = 0;
    [self.mainLabel sizeToFit];
    self.mainLabel.text = self.gifModel.content;
    
    return view;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:0.7 green:0.9 blue:0.7 alpha:0.9];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cell.contentView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.9 ];
    });
//    UIViewController * vc = [[UIViewController alloc]init];
//    UIWebView * webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
//    [vc.view addSubview:webView];
//    NSURL * url = [NSURL URLWithString:@"http://gif.duowan.com/view/155822"];
//    [webView loadRequest:[NSURLRequest requestWithURL:url]];
//    UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:vc action:@selector(dismissViewControllerAnimated:completion:)];
//    [webView addGestureRecognizer:swipe];
//    [self presentViewController:vc animated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    static float i = 1;
    float x = -scrollView.contentOffset.y*1.0/(scrollView.contentSize.width);
    if (i+x>1) {
    }
    
    
}
-(void)didClickBackButton:(UIBarButtonItem *)leftBarButton{

    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
